﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace shop.Areas.admin.Controllers
{
    public class accountController : Controller
    {
        private LongHAEntities db = new LongHAEntities();

        //
        // GET: /admin/account/
        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("index", "home");
        }
         public ActionResult login()
        {
            return View("login");
        }
         [HttpPost]
         public ActionResult login(Taikhoan acc, string returnUrl)
         {
             if (ModelState.IsValid)
             {

                 var tk = db.Taikhoans.Where(s => s.tentk.Equals(acc.tentk) && s.matkhau.Equals(acc.matkhau));
                 if (tk.Count() != 0)
                 {
                     FormsAuthentication.SetAuthCookie(acc.tentk, true);
                     //FormsAuthentication.Timeout.Hours = 5;
                     //FormsAuthentication.SetAuthCookie(acc.TaiKhoan, false);
                     if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                         && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))//doan code nay nham muc dich khi ma ban vao mot trang nao do mo khong dung quyen han no se bat ban dang nhap.neu thanh cong no se tro lai trang truoc do ma ban muon vao.
                     {
                         return Redirect(returnUrl); //tro lai trang truoc do ma ban muon vao
                     }
                     else
                     {
                         return RedirectToAction("index", "home");
                     }
                 }
                 else
                 {
                     ModelState.AddModelError("", "Tài khoản hoặc mật khẩu không đúng");
                 }
             }


             return View("login", acc);
         }
       
        public ActionResult Index()
        {
            return View(db.Taikhoans.ToList());
        }

        //
        // GET: /admin/account/Details/5

        public ActionResult Details(int id = 0)
        {
            Taikhoan taikhoan = db.Taikhoans.Find(id);
            if (taikhoan == null)
            {
                return HttpNotFound();
            }
            return View(taikhoan);
        }

        //
        // GET: /admin/account/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /admin/account/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Taikhoan taikhoan)
        {
            if (ModelState.IsValid)
            {
                db.Taikhoans.Add(taikhoan);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(taikhoan);
        }

        //
        // GET: /admin/account/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Taikhoan taikhoan = db.Taikhoans.Find(id);
            if (taikhoan == null)
            {
                return HttpNotFound();
            }
            return View(taikhoan);
        }

        //
        // POST: /admin/account/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Taikhoan taikhoan)
        {
            if (ModelState.IsValid)
            {
                db.Entry(taikhoan).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(taikhoan);
        }

        //
        // GET: /admin/account/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Taikhoan taikhoan = db.Taikhoans.Find(id);
            if (taikhoan == null)
            {
                return HttpNotFound();
            }
            return View(taikhoan);
        }

        //
        // POST: /admin/account/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Taikhoan taikhoan = db.Taikhoans.Find(id);
            db.Taikhoans.Remove(taikhoan);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}